#!/tools/bin/bash

# temel dosya sistemini ilklemek, tools a chroot olurken 1 kere kullanılır.
# yanlışlıkla önsistem içinde yapılmasını önlemek için
[ ! -z $ONSISTEM_CHROOT ] && exit 1

mkdir -pv /{boot,etc/{opt,sysconfig},home,mnt,opt}
mkdir -pv /{media/{floppy,cdrom},srv,var}
install -dv -m 0750 /root
install -dv -m 1777 /tmp /var/tmp
mkdir -pv /usr/{,local/}{bin,include,lib,lib/firmware,src}
mkdir -pv /usr/{,local/}share/{color,dict,doc,info,locale,man}
mkdir -v  /usr/{,local/}share/{misc,terminfo,zoneinfo}
mkdir -pv /usr/{,local/}share/man/man{1..8}
ln -sf /usr/bin /bin
ln -sf /usr/bin /usr/sbin
ln -sf /usr/bin /sbin
case $(uname -m) in
 x86_64) 
	ln -sf /usr/lib /lib
	ln -sf /usr/lib /lib64
	ln -sf /usr/lib /usr/lib64
 ;;
esac

mkdir -v /var/{log,mail,spool}
ln -sv /run /var/run
ln -sv /run/lock /var/lock
mkdir -pv /var/{opt,cache,lib/{color,misc,locate},local}

ln -sv /tools/bin/{bash,cat,dd,echo,ln,pwd,rm,stty} /usr/bin
ln -sv /tools/bin/{env,install,perl}                /usr/bin
ln -sv /tools/lib/libgcc_s.so{,.1}                  /usr/lib
ln -sv /tools/lib/libstdc++.{a,so{,.6}}             /usr/lib

install -vdm755 /usr/lib/pkgconfig

ln -sv bash /usr/bin/sh

ln -sv /proc/self/mounts /etc/mtab

cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
daemon:x:6:6:Daemon User:/dev/null:/bin/false
messagebus:x:18:18:D-Bus Message Daemon User:/var/run/dbus:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
EOF

cat > /etc/group << "EOF"
root:x:0:
bin:x:1:daemon
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
adm:x:16:
messagebus:x:18:
input:x:24:
mail:x:34:
kvm:x:61:
wheel:x:97:
nogroup:x:99:
users:x:999:
EOF

touch /var/log/{btmp,lastlog,faillog,wtmp}
chgrp -v utmp /var/log/lastlog
chmod -v 664  /var/log/lastlog
chmod -v 600  /var/log/btmp

# talimatname yolu 
mkdir -p /usr/milis/talimatname
[ -d /tools/share/talimatname ] && cp -rf /tools/share/talimatname/* /usr/milis/talimatname/

# eski onsistem paketlerini içeren db i pasif etmek
mv /var/lib/mps/db /var/lib/mps/db.onsistem
mkdir /var/lib/mps/db
mv /var/log/mps /var/log/mps.onsistem
mkdir -p /var/log/mps/kur
mkdir -p /var/log/mps/sil

# resolv.conf net için
[ -f /tools/etc/resolv.conf ] && cp -fv /tools/etc/resolv.conf /etc/

# hazır betikler - milis-rootfs gibi paketle sunulacak
[ -d /tools/share/bin ] && cp -rf /tools/share/bin/* /usr/bin/

# +h direktifiyle iç yol hashlemesini kullanmayacak
# bu direktif verilmezse bash çalıştırdığı ikilileri hatırlayabilir
exec /tools/bin/bash --login +h
